/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package construtora.limiar.dao;

import construtora.limiar.dao.generic.AbstractDAO;
import construtora.limiar.dao.generic.DAOEngine;
import construtora.limiar.dao.generic.IDAO;
import construtora.limiar.models.Usuario;
import construtora.limiar.models.generic.IModel;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author michel
 */
public class UsuarioDAO extends AbstractDAO implements IDAO{

    @Override
    public String[][] getFields(IModel model) {
        Usuario usuario = (Usuario) model;
        String[][] fields = new String[4][2];
        fields[0][0] = "apelido";
        fields[0][1] = "'" + usuario.getApelido() + "'";
        fields[1][0] = "senha";
        fields[1][1] = "'" + usuario.getSenha() + "'";
        fields[2][0] = "email";
        fields[2][1] = "'" + usuario.getEmail() + "'";        
        fields[3][0] = "role";
        fields[3][1] = "'" + usuario.getRole() + "'";
        return fields;
    }

    @Override
    public IModel fetchOne(IModel model) {
        String where = this.getDefaultWhere(model);
        List<Usuario> list = (List<Usuario>) this.fetchAll(where, null, null, null);
        if (list != null && list.size() > 0)
            return list.get(0);
        else
            return null;

    }

    @Override
    public List<? extends IModel> fetchAll(String where, String order, Integer limit, Integer offset) {
        List<Usuario> list = new ArrayList<Usuario>();
        ResultSet rs = DAOEngine.fetchAll(this.table, where, order, limit, offset);
        try {
            while (rs.next()){
                Usuario usuario = new Usuario();
                usuario.setId(rs.getInt("id_usuario"));
                usuario.setApelido(rs.getString("apelido"));
                usuario.setSenha(rs.getString("senha"));
                usuario.setEmail(rs.getString("email"));
                usuario.setRole(rs.getString("role"));
                list.add(usuario);
            }
            return list;
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public UsuarioDAO(){
        this.table = "usuario";
    }
    
    public void criarUsuario(Usuario usuario){
        String sql1, sql2, sql3; 
        if(!"operario".equals(usuario.role)){
            sql1 = "CREATE USER '"+ usuario.apelido+"'@'localhost' identified by '"+usuario.senha+"'";
            sql2 = "GRANT ALL PRIVILEGES ON * . * TO  '"+usuario.apelido+"'@'localhost' IDENTIFIED BY  '"+usuario.senha+"' WITH GRANT OPTION;";
            sql3 = "GRANT ALL PRIVILEGES ON `projlimiar` .* TO '"+usuario.apelido+"'@'localhost' WITH GRANT OPTION;";
                
            DAOEngine.executeSQL(sql1);
            DAOEngine.executeSQL(sql2);
            DAOEngine.executeSQL(sql3);
        }
        
        
    }
}
